!function(HTMLTurbo){
	var PatternBuilder=HTMLTurbo.util.PatternBuilder;
	
	var Form=function(selector){
		this.selector=selector;
		this.element=$(selector).get(0);
	}
	Form.prototype.rule=null;
	Form.prototype.eventList=null;
	Form.prototype.submitRuleList=null;
	Form.prototype.ruleNameList=null;
	Form.prototype.ruleUrl="./formRule.json";
	
	Form.prototype.ruleUrl
	Form.prototype.ruleUrl
	
	Form.prototype.init=function(){
		//关闭验证 测试
		//return false;
		var self=this;
		$(this.selector).bind("submit",function(event){return self.submitHandler(event)})
		this.loadRule(this.ruleUrl);
	}
	Form.prototype.loadRule=function(url){
		var self=this;
		var setting={}
		setting.url=url;
		setting.dataType="json"
		setting.cache=false
		setting.success=function(data, textStatus, jqXHR){
			self.rule=data;
			self.update();
		},
		setting.error=function(){
			alert("规则加载失败");
		}
		$.ajax(setting);
	}
	Form.prototype.update=function(){
		this.updateSubmitRuleNameList();
	}
	Form.prototype.updateSubmitRuleNameList=function(){
		var ruleNameList=new Array();
		ruleNameList.push("base");
		this.updateSubmitRuleList(ruleNameList);
	}
	Form.prototype.updateSubmitRuleList=function(ruleNameList){
		var submitRuleList=this.rule.submitRuleList;
		for(j in ruleNameList){
			var ruleName=ruleNameList[j];
			for(i in submitRuleList){
				if(submitRuleList[i].name==ruleName){
					var ruleList=submitRuleList[i].fieldList;
				}
			}
		}
		//var ruleList=this.ruleData.submitRules.base;
		this.submitRuleList=ruleList;
		var element=null;
		var rule=null;
		for(var i=0;i<ruleList.length;i++){
			rule=ruleList[i];
			this.updateSubmitRule(rule);
		}
	}
	Form.prototype.updateSubmitRule=function(rule){
		var label=rule.label;
		var name=rule.name;
		var required=rule.required;
		var pattern=rule.pattern;
		var patternName=rule.patternName;
		var title=rule.title;
		var type=rule.type;
		var tips=rule.tips;
		
		var elements=$("[name=\""+name+"\"]");
		
		//生成规则
		if(patternName!=""){
			var evalStr="PatternBuilder."+patternName;
			if(pattern==""){
				pattern=eval(evalStr);
			}
		}
		var isVisible=$(elements).is(":visible")
		//附加规则
		if(pattern!=""){
			if(type!="select"){
				if(isVisible){
					elements.attr("pattern",pattern);
				}
			}
		}
		//增加必填
		if(required){
			if(isVisible){
				elements.attr("required","required");
			}
		}
		
		//增加提示
		if(title){
			elements.attr("title",title);
		}
		
		//增加字段名称
		var labelElement=this.getLabelElement(elements);
		if(labelElement){
			var labelHtml="";
			labelHtml="<span class=\"required\">*</span>"+label+"：";
			labelElement.html(labelHtml);
		}
		
		//增加提示
		if(tips!=""){
			var tipsElement=this.getTipsElement(elements);
			if(tipsElement){
				var tipsHtml="";
				tipsHtml=tips;
				tipsElement.html(tipsHtml);
			}
		}
	}
	Form.prototype.getLabelElement=function(elements){
		var labelElement=elements.parent().find("label");
		return labelElement;
	}
	Form.prototype.getTipsElement=function(elements){
		var tipsElement=elements.parent().find(".tips");
		if(tipsElement.length==0){
			tipsElement=$("<p class=\"tips\"></p>");
			elements.parent().append(tipsElement);
		}
		return tipsElement;
	}
	Form.prototype.getData=function(){
		/*
		//自动填充frame数据
		var frameInputs=$("[type=frame]",this.element)
		var name="";
		var value="";
		for(var i=0;i<frameInputs.length;i++){
			name=frameInputs.get(i).name;
			value=this.getFrameValue("#"+name+"Frame");
			frameInputs.eq(i).val(value);
		}
		*/
		var serializeArray=$(this.element).serializeArray();
		return serializeArray;
	}
	Form.prototype.submitHandler=function(event){
		var self=this;
		try{
			if(!self.validate()){
				self.alertEventList(self.eventList);
				//设置焦点
				if(self.eventList.length>0){
					var focusTarget=self.eventList[0].target
					$(focusTarget).focus();
				}
				return false;
			}
			return true;
		}catch(e){
			console.log(e);
			alert("提交失败");
			return false;
		}
	}
	Form.prototype.alertEventList=function(eventList){
		var event=null;
		var temp="";
		console.log(eventList)
		for(var i=0;i<eventList.length;i++){
			event=eventList[i]
			tips=event.tips;
			temp=tips+"\n";
		}
		eventList=new Array();
		alert(temp);
	}
	Form.prototype.validate=function(){
		var dataList=this.getData();console.log(dataList);
		var ruleList=this.submitRuleList;
		var rule=null;
		var value=null;
		var target=null;
		
		var name,label,type,required,pattern,patternName,placeholder,title,tips,maxLength;

		var eventList=new Array();
		var event=null;
		
		for(var i=0;i<ruleList.length;i++){
			rule=ruleList[i]
			name=rule.name;
			if(name=="sourcePassword"){
				continue;
			}
			type=rule.type;
			required=rule.required;
			label=rule.label;
			pattern=rule.pattern;
			patternName=rule.patternName;
			title=rule.title;
			
			value=this.getValueByName(dataList,name);
			target=$("[name="+name+"]",this.element);
			
			//事件提示语
			var eventTips="";
			if(required){
				if(type=="select"){
					eventTips=label+" 必选 "+title;
				}else{
					eventTips=label+" 必填 "+title;
				}
			}else{
				eventTips=label+" "+title;
			}
			//必填验证
			if(required){
				if(value==""){
					event=new Event();
					event.tips=eventTips
					event.target=target;
					eventList.push(event);
					continue;
				}
			}
			//规则验证
			if(patternName!=""){
				var evalStr="PatternBuilder."+patternName;
				if(pattern==""){
					pattern=eval(evalStr);
				}
			}
			if(pattern!=""){
				//if(type!="select"){
					var regExp=new RegExp(pattern);
					console.log(value);
					console.log(pattern);
					if(!regExp.test(value)){
						event=new Event();
						event.tips=eventTips
						event.target=target;
						eventList.push(event);
						continue;
					};
				//}
			}
		}
		this.eventList=eventList;
		if(eventList.length==0){
			return true;	
		}else{
			return false;
		}
	}
	

	var Event=function(){}
	Event.prototype.tips="";
	Event.prototype.code=200;
	Event.prototype.target=null;
	
	Form.prototype.getValueByName=function(dataList,name){
		var value;
		var data;
		for(var i=0;i<dataList.length;i++){
			data=dataList[i]
			if(data.name==name){
				value=data.value;
			}
		}
		return value;
	}
	HTMLTurbo.control.Form=Form;
}(HTMLTurbo)